/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * PTDScriptPane.java
 *
 * Created on November 8, 2005, 8:07 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package ptd;

import javax.swing.JOptionPane;
import ssi_colorizer.*;
import ssi_linenumberview.*;
import javax.swing.ImageIcon;
import java.awt.Rectangle;
import java.awt.Point;
import java.util.Vector;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


/**
 *
 * @author John
 */
public class PTDScriptPane extends SSI_ColorPane{
    
    private PTDScript MyScript;
    public PTDDebugPanel MyDebug;
    public LineNumberView MyLineNumView;
    
    public LineIcon MyLinePtr;
    
    public Vector<LineIcon> MyBptPtrs;
    private ImageIcon BptIcon;
    
    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
    public class LineNumberMouseListener extends MouseAdapter{
      // -----------------------------------------------------
      public void mousePressed(MouseEvent e) {
        // Required to allow keyboard navigation when a tree item is selected.
      
        int iH = MyLineNumView.getLineHeight(1);
        int LineNum = (e.getPoint().y / iH) + 1;
              
        toggleBreakpoint(LineNum);
        MyLineNumView.update();
        //MyBptPtr.Set(LineNum, null);
        //MyLineNumView.update();
      
        if(e.getButton() == e.BUTTON3){
        }
      }
      // -----------------------------------------------------
      public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
        }
      }
    }


    
    
    /** Creates a new instance of PTDScriptPane */
    public PTDScriptPane() {
        MyLineNumView = new LineNumberView(this);
        MyScript = null;
        MyDebug = null;
        
        MyLinePtr = new LineIcon();      
        
        ImageIcon a = PTDImageLoader.getImageIcon(PTDImageLoader.IMG_CUR_LINE);
        MyLinePtr.Set(0, a.getImage());
        
        BptIcon = PTDImageLoader.getImageIcon(PTDImageLoader.IMG_BPT_LINE);
        //MyBptPtr.Set(0, b.getImage());
        MyBptPtrs = new Vector<LineIcon>();

        // Order here matters...
        //MyLineNumView.IconMgr.addIcon(MyBptPtr);
        MyLineNumView.IconMgr.addIcon(MyLinePtr);
        
        MyLineNumView.addMouseListener(new LineNumberMouseListener());
        bTrackIndents = true;
    }
    
    public void setScript(PTDScript NewScript){
        MyScript = NewScript;
    }
    
    public PTDScript getScript(){
        return MyScript;
    }
    
    public void scrollToLine(int Line){
        int LineH = MyLineNumView.getLineHeight(Line);
        Point p = new Point(0, (Line-1)*LineH);
        int sLine = MyLineNumView.firstVisibleLine;
        int eLine = MyLineNumView.lastVisibleLine;
        
        if((Line <= sLine) || (Line > eLine)){
            Rectangle r = new Rectangle(p.x, p.y-LineH, 1, 3*LineH);
            this.scrollRectToVisible(r);
        }else{
            
        }
    }
    
    public void clearDebugPtr(){
        MyLinePtr.Set(-1, null);
        MyLineNumView.update();
    }
    
    public void toggleBreakpoint(int LineNum){
        int sz = MyBptPtrs.size();
        int ctr = 0;
        int EmptyInd = -1;
        LineIcon anIcon;
        
        for(ctr = 0; ctr < sz; ctr++){
            anIcon = (LineIcon)MyBptPtrs.get(ctr);
            
            if(anIcon.m_LineNum == LineNum){
                if(MyDebug != null){
                    MyDebug.removeBptCmd(LineNum, MyScript.myFile);
                }

                anIcon.Set(-1, null);
                MyLineNumView.repaint();
                return;
            }else if(anIcon.m_LineNum < 0){
                EmptyInd = ctr;
            }
        }

        if(MyDebug != null){
            MyDebug.addBptCmd(LineNum, MyScript.myFile);
        }

        // Must do it this way, else will add new bpt before it finds existing one...
        if(EmptyInd > 0){
            anIcon = (LineIcon)MyBptPtrs.get(EmptyInd);
            anIcon.Set(LineNum, null);
            MyLineNumView.repaint();
            return;
        }
        
        anIcon = new LineIcon();
        anIcon.Set(LineNum, BptIcon.getImage());
        MyLineNumView.IconMgr.addIcon(anIcon);
        MyBptPtrs.add(anIcon);
        MyLineNumView.repaint();
        return;
    }
    
    public void toggleBreakpoint(){
        int iPos = getCaretPosition();
        iPos = lineFromPos(iPos);
        if(iPos <= 0){
            return;
        }
        toggleBreakpoint(iPos);
    }
    
    public boolean closeMe(){
        return saveMeConfirm();
    }
    
    public boolean saveMe(){
        PTDScript SelScript = getScript();
        if(SelScript.myFile == null){
            return PTDMain.performSaveAs(this);
        }
        if(saveFile(MyScript.myFile) == 1){
            return true;
        }
        return false;
    }
    
    public boolean saveMeConfirm(){
        if(bChanged){
            String FileName = "Untitled";
            if(this.MyScript != null){
                if(MyScript.myName != null){
                    FileName = MyScript.myName;
                }
            }

            int iSel = PTDMain.saveConfirm("The script '" + FileName + "' has been modified.  Do you want to save it?", "Script Modified");
            //JOptionPane aPane = new JOptionPane();
            //aPane.setMessageType(aPane.QUESTION_MESSAGE);
            //aPane.setOptionType(aPane.YES_NO_CANCEL_OPTION);
            //int iSel = aPane.showConfirmDialog(this, "The script '" + FileName + "' has been modified.  Do you want to save it?", "Script Modified", aPane.YES_NO_CANCEL_OPTION);
            if(iSel == JOptionPane.CANCEL_OPTION){
                return false;
            }else if(iSel == JOptionPane.YES_OPTION){
                return saveMe();
            }else{
                return true;
            }
        }else{
            return true;
        }
    }
    
    
}
